package common.controller;

import jhunter.web.mvc.controller.BaseController;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginController extends BaseController {

	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Override
	public Object execute() {
		Subject currentUser = SecurityUtils.getSubject();
		
		String username = (String) this.getParams().get("username");
		String password = (String) this.getParams().get("password");

		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		try {
			currentUser.login(token);
//			Object obj = currentUser.getPrincipal();
//			Session session = currentUser.getSession();
//			session.setAttribute("someKey", "aValue");
		} catch (AuthenticationException e) {
			log.debug("Login exception: {}", e.getMessage());
			this.getFlash().put("error", "账号或密码错误，请重新输入.");
			return rAction("login");
		}
		return rAction("index","mainFrame");
	}
}
